package com.qf.dao;

import com.qf.entity.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * --- 缘分让我们相遇乱世以外 ---
 *
 * @author SJY
 * @date 2024/11/8
 * @desc
 */
public class UserDaoImpl implements UserDao {
    @Override
    public User login(String username, String password) {
        Connection conn = null;
        PreparedStatement ps = null;
        User user = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2405?useSSL=false", "root", "123456");
            ps = conn.prepareStatement("select * from tb_user where username = ? and password = ?");
            ps.setString(1, username);
            ps.setString(2, password);
            rs = ps.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setCreateTime(rs.getDate("create_time"));
                //....
            }

        } catch (Exception e) {
            System.out.println("登录的bug");
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                ps.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    @Override
    public boolean regist(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2405?useSSL=false", "root", "123456");
            ps = conn.prepareStatement("insert into tb_user (username,password,phone,create_time,sex) values (?,?,?,?,?)");
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getPhone());
            ps.setDate(4, new java.sql.Date(user.getCreateTime().getTime()));
            ps.setInt(5, user.getSex());
            int i = ps.executeUpdate();
            return 1 > 0 ? true : false;
        } catch (Exception e) {
            System.out.println("登录bug");
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                conn.close();
            } catch (Exception e) {
                System.out.println("关闭连接时出现问题");
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public ArrayList<User> findAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        User user = null;
        ResultSet rs = null;
        ArrayList<User> list = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2405?useSSL=false", "root", "123456");
            ps = conn.prepareStatement("select * from tb_user");
            rs = ps.executeQuery();// 使用预处理语句对数据库进行查询
            while (rs.next()) {
                user = new User();// 创建user对象对映射类进行
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setCreateTime(rs.getDate("create_time"));
                user.setId(rs.getInt("id"));
                user.setSex(rs.getInt("sex"));
                user.setPhone(rs.getString("phone"));

                list.add(user);// 将查询出的语句放进集合中
            }
        } catch (Exception e) {
            // 抛出异常并且输出原因
            System.out.println("登录bug");
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                ps.close();
                conn.close();
            } catch (Exception e) {
                System.out.println("关闭连接出问题");
                e.printStackTrace();
            }
        }
        return list;// 将集合中的数据返回给控制层
    }
}
